package puzzle;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class PuzzleMain {

	private static ArrayList<PuzzlePiece> pieces = new ArrayList<PuzzlePiece>();
	
	public static PieceEdge parseEdge(String edge){
	      char[] edgeArray;
	      char colour;
	      char body;
	      char ori;
	      
	      edgeArray = edge.toCharArray();
	      colour = edgeArray[0];
	      body = edgeArray[1];
	      ori = edgeArray[2];
	      if(body != 'h' && body != 't'){
	         System.out.println("Invalid body type: " + body);
	         return null;
	      }
	      if(ori != 'l' && ori != 'r'){
	         System.out.println("Invalid orientation: " + ori);
	         return null;
	      }

	      /*CREATE PUZZLE EDGE FOR PUZZLE PIECE*/
	      PieceEdge pEdge = new PieceEdge(colour, body, ori);
	      
	      return pEdge;
	   }
	
	public static PuzzlePiece parseLine(String line){
	      char[] lineArray;
	      String edge = "";
	      int num_edges = 0;
	      ArrayList<PieceEdge> pEdges = new ArrayList<PieceEdge>();
	      
	      line = line.toLowerCase();
	      line = line.trim();
	      lineArray = line.toCharArray();
	      if(lineArray[0] != '{'){
	         System.out.println("Expected {, found: " + lineArray[0]);
	         return null;
	      }
	      for(int i = 1; i < lineArray.length; i++){
	         if(lineArray[i] != ',' && lineArray[i] != '}'){
	            if(lineArray[i] != ' '){
	               edge += lineArray[i];
	            }
	         }
	         else {
	            if(edge.length() == 3 && num_edges < 3){
	               num_edges++;
	               PieceEdge pEdge = parseEdge(edge);
	               if(pEdge == null){
	                  return null;
	               }
	               /* ADD EDGE TO ARRAYLIST */
	               pEdges.add(pEdge);
	               edge = "";
	            }
	            else {
	               if(num_edges == 3){
	                  System.out.println("Too many Edges: " + edge);
	               }
	               else {
	                  System.out.println("Invalid Edge: " + edge);
	               }
	               return null;
	            }
	         }
	      }
	      /*CREATE PUZZLE EDGE FOR PUZZLE PIECE*/
	      return new PuzzlePiece(pEdges);
	}
	
	public static void main(String[] args){
		System.out.println("Hi mom!\n");
		
		try {   
	         FileReader input = new FileReader("bin/puzzle/octohedronA");
	         BufferedReader bufRead = new BufferedReader(input);
	          
	         String line;
	         int count = 0;
	           
	         line = bufRead.readLine();
	         count++;
	         
	         while (line != null){
	        	 PuzzlePiece pPiece = parseLine(line);
	            if(pPiece == null){
	               return;
	            }
	            pieces.add(pPiece);
	            line = bufRead.readLine();
	            count++;
	         }
	            
	         bufRead.close();
	         
	      }catch (ArrayIndexOutOfBoundsException e){
	         System.out.println("Usage: java ReadFile filename\n");          
	      }catch (IOException e){
	         e.printStackTrace();
	      }
	      
		for(int i = 0; i < pieces.size(); i++){
			System.out.println("Piece " + (i+1));
			System.out.println(pieces.get(i));
		}
		
		System.out.println("Bye mom!");
	}
	
}
